package com.huobao.yunying.activitys.muyingquan;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.xutils.view.annotation.ViewInject;

import com.huobao.yunying.adapters.ViewPagerAdapter;
import com.huobao.yunying.base.HuobaoBaseActivity;
import com.huobao.yunying.utils.DialogUtils;
import com.huobao.yunying.utils.DialogUtils.DialogListener;
import com.huobao.yunying.utils._PopupImageString;
import com.huobao.zhangying.R;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

public class Act_SheQu_XiaoXi extends HuobaoBaseActivity implements OnClickListener {
	@ViewInject(R.id.rel_1)
	private RelativeLayout Rel1;
	@ViewInject(R.id.rel_2)
	private RelativeLayout Rel2;
	@ViewInject(R.id.viewpager)
	private ViewPager mViewPager;
	private LocalActivityManager manager = null;
	private ViewPagerAdapter adapter;
	private List<View> views;
	@ViewInject(R.id.shequxiaoxi_back)
	private Button Btn_Back;
	@ViewInject(R.id.shequxiaoxi_more)
	private Button Btn_More;
	protected _PopupImageString popUtils;
	@ViewInject(R.id.lin_shequxiaoxi)
	private LinearLayout mLinearLayout;
	@ViewInject(R.id.tv_1)
	private TextView Tv_1;
	@ViewInject(R.id.tv_2)
	private TextView Tv_2;
	@ViewInject(R.id.xiaoxi1)
	private TextView Tv_XiaoXi1;
	private int Tv1_width = 0, Tv1_height = 0;

	@Override
	protected int getContentView() {
		return R.layout.layout_shequ_xiaoxi;
	}

	@Override
	protected String getTitleText() {
		return "";
	}

	private View getView(String id, Intent intent) {
		return manager.startActivity(id, intent).getDecorView();
	}

	@SuppressWarnings("deprecation")
	@Override
	protected void init() {
		mAbTitleBar.setVisibility(View.GONE);
		manager = new LocalActivityManager(this, true);
		manager.dispatchCreate(savedInstanceState);
		Rel1.setOnClickListener(this);
		Rel2.setOnClickListener(this);
		Btn_Back.setOnClickListener(this);
		Btn_More.setOnClickListener(this);
		Rel1.setTag(true);
		Rel2.setTag(false);
		Tv_1.setTextColor(getResources().getColor(R.color.shequxiao_checked));
		Tv_2.setTextColor(getResources().getColor(R.color.shequxiao_unchecked));
		// 设置提醒点的宽高
		// 在oncreate中计算TextView的宽高
		Tv_1.post(new Runnable() {
			@Override
			public void run() {
				Tv1_width = Tv_1.getWidth();
				Tv1_height = Tv_1.getHeight();
				LayoutParams params = new LayoutParams(Tv1_width / 2, Tv1_height / 2 + 10);
				params.addRule(RelativeLayout.CENTER_VERTICAL);
				params.addRule(RelativeLayout.RIGHT_OF, R.id.tv_1);
				Tv_XiaoXi1.setLayoutParams(params);
			}
		});

		views = new ArrayList<View>();
		Intent i1 = new Intent(this, Act_SheQuXiaoXi_HuiFu.class);
		views.add(getView("A", i1));
		Intent i2 = new Intent(this, Act_SheQuXiaoXiMy.class);
		views.add(getView("B", i2));
		adapter = new ViewPagerAdapter(views);
		mViewPager.setAdapter(adapter);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				switch (arg0) {
				case 0:
					Rel1.setBackgroundResource(R.mipmap.tab_selected_holo);
					Rel2.setBackgroundResource(R.mipmap.tab_unselected_holo);
					Tv_1.setTextColor(getResources().getColor(R.color.shequxiao_checked));
					Tv_2.setTextColor(getResources().getColor(R.color.shequxiao_unchecked));
					Rel2.setTag(false);
					Rel1.setTag(true);
					break;
				case 1:
					Rel2.setBackgroundResource(R.mipmap.tab_selected_holo);
					Rel1.setBackgroundResource(R.mipmap.tab_unselected_holo);
					Tv_1.setTextColor(getResources().getColor(R.color.shequxiao_unchecked));
					Tv_2.setTextColor(getResources().getColor(R.color.shequxiao_checked));
					Rel2.setTag(false);
					Rel1.setTag(true);
					break;
				}
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {

			}

			@Override
			public void onPageScrollStateChanged(int arg0) {

			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.rel_1:
			boolean flag = (Boolean) Rel1.getTag();
			if (flag) {
				Rel1.setBackgroundDrawable(getResources().getDrawable(R.mipmap.tab_selected_holo));
				Rel2.setBackgroundDrawable(getResources().getDrawable(R.mipmap.tab_unselected_holo));
				Rel2.setTag(false);
				Rel1.setTag(true);
				mViewPager.setCurrentItem(0);
				Tv_1.setTextColor(getResources().getColor(R.color.shequxiao_checked));
				Tv_2.setTextColor(getResources().getColor(R.color.shequxiao_unchecked));
			} else {
				Rel1.setBackgroundDrawable(getResources().getDrawable(R.mipmap.tab_selected_holo));
				Rel2.setBackgroundDrawable(getResources().getDrawable(R.mipmap.tab_unselected_holo));
				Rel1.setTag(true);
				Rel2.setTag(false);
				mViewPager.setCurrentItem(0);
				Tv_1.setTextColor(getResources().getColor(R.color.shequxiao_checked));
				Tv_2.setTextColor(getResources().getColor(R.color.shequxiao_unchecked));
			}
			break;
		case R.id.rel_2:
			boolean flag1 = (Boolean) Rel2.getTag();
			if (flag1) {
				Rel2.setBackgroundDrawable(getResources().getDrawable(R.mipmap.tab_selected_holo));
				Rel1.setBackgroundDrawable(getResources().getDrawable(R.mipmap.tab_unselected_holo));
				Tv_2.setTextColor(getResources().getColor(R.color.shequxiao_checked));
				Tv_1.setTextColor(getResources().getColor(R.color.shequxiao_unchecked));
				Rel2.setTag(false);
				Rel1.setTag(true);
				mViewPager.setCurrentItem(1);
			} else {
				Rel1.setBackgroundDrawable(getResources().getDrawable(R.mipmap.tab_unselected_holo));
				Rel2.setBackgroundDrawable(getResources().getDrawable(R.mipmap.tab_selected_holo));
				Tv_2.setTextColor(getResources().getColor(R.color.shequxiao_checked));
				Tv_1.setTextColor(getResources().getColor(R.color.shequxiao_unchecked));
				Rel2.setTag(true);
				Rel1.setTag(false);
				mViewPager.setCurrentItem(1);
			}
			break;
		case R.id.shequxiaoxi_back:
			finish();
			break;
		case R.id.shequxiaoxi_more:
			showPop();
			break;
		}
	}

	@SuppressWarnings("static-access")
	private void showPop() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map;

		map = new HashMap<String, Object>();
		map.put("text", "全部标记为已读");
		map.put("image", R.mipmap.all_read);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("text", "清空消息");
		map.put("image", R.mipmap.clear_all);
		list.add(map);

		map = new HashMap<String, Object>();
		map.put("text", "跳页");
		map.put("image", R.mipmap.tiaoye);
		list.add(map);
		popUtils.showPop(context, list, mLinearLayout, new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				popUtils.dismiss();
				switch (position) {
				case 0:
					// 全部删除
					break;
				case 1:
					break;
				case 2:
					// 跳到第几页
					DialogUtils.ShowTiaoYeDialog(context, new DialogListener() {
						@Override
						public void OnSubmit(String msg) {

						}

						@Override
						public void OnCancle() {

						}
					});
					break;
				}
			}
		});
	}
}
